package edu.cuit.svsp.controller;

import edu.cuit.svsp.entity.User;
import edu.cuit.svsp.request.ResponseResult;
import edu.cuit.svsp.service.UserService;
import edu.cuit.svsp.util.JWTUtil;
import edu.cuit.svsp.util.OssUtil;
import edu.cuit.svsp.vo.AvatarVo;
import edu.cuit.svsp.vo.UserLoginVo;
import edu.cuit.svsp.vo.UserSearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @className: UserController
 * @author: pjy
 * @description: 用户接口
 * @date: 2024/7/2
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private OssUtil ossUtil;
    //用户登录
    @PostMapping("/login")
    public ResponseResult<User> login(@RequestBody UserLoginVo userLoginVo, HttpServletResponse response){
        User user=userService.login(userLoginVo);
        //生成token
        String token= JWTUtil.generateToken(user.getId(),user.getAccount());
        response.setHeader("Authorization",token);
        return new ResponseResult<>(HttpStatus.OK.value(), "登录成功",user);
    }

    //搜索用户
    @PostMapping("/search")
    public ResponseResult search(@RequestBody UserSearchVo vo){
//        log.info("搜索用户"+vo.toString());
        return new ResponseResult<>(HttpStatus.OK.value(), "success",userService.search(vo));
    }

    //锁定用户状态
    @PostMapping("/lock/{id}")
    public ResponseResult lock(@PathVariable("id") Integer id){
        return new ResponseResult<>(HttpStatus.OK.value(), "success",userService.lock(id));
    }

    //解锁用户状态
    @PostMapping("/pass/{id}")
    public ResponseResult pass(@PathVariable("id") Integer id){
        return new ResponseResult<>(HttpStatus.OK.value(), "success",userService.pass(id));
    }

    //修改用户等级
    @PostMapping("/updateLevel/{id}/{level}")
    public ResponseResult updateLevel(@PathVariable("id") Integer id,@PathVariable("level") String level){
        return new ResponseResult<>(HttpStatus.OK.value(), "success",userService.updateLevel(id,level));
    }

    //修改密码
    @PostMapping("/updatePassword")
    public ResponseResult updatePassword(@RequestBody Map<String,String> map){
//        log.info("修改密码"+map.toString());
        if(!userService.updatePassword(Integer.parseInt(map.get("id")),map.get("old"),map.get("new"))){
            return new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "密码错误",false);
        }
        return new ResponseResult<>(HttpStatus.OK.value(), "success",true);
    }

    //修改头像
    @PostMapping("/updateAvatar")
    public ResponseResult updateAvatar(AvatarVo avatar, HttpServletRequest request) throws IOException {
        int uid = JWTUtil.getuid(request.getHeader("Authorization"));
        String avatarUrl = ossUtil.upOss(avatar.getAvatar());
        boolean b = userService.updateAvatar(avatarUrl, uid);
        if(!b){
            return new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "修改失败",false);
        }else {
            return new ResponseResult<>(HttpStatus.OK.value(), "success", userService.findById(uid));
        }
    }
}
